# !/usr/bin/python
# -*-coding: utf-8-*-
# Filename: Socket_Client.py

import socket
import threading

import H_socket

# IP
CONNECT_IP = '192.168.1.55'
# 端口
CONNECT_PORT = 5010


# 地址设定
Address = ( CONNECT_IP, CONNECT_PORT )
# socket设定 SOCK_STREAM - TCP
Socket_Client = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
# socket连接建立
if( 0 != Socket_Client.connect_ex( Address ) ):
	print( 'socket connect error!' )
	exit(1)

# Log文件打开
ClientLogFile = open('C_Log.log', 'a' )

# socket送信
Socket_Client.send( b'Hello Python-Socket! my is client.' )

try :
	# 读取线程建立
	RecvThread = threading.Thread( target = H_socket.Socket_Recv, args = ( Socket_Client, Address, ClientLogFile ) )
	# 送信线程建立
	SendThread = threading.Thread( target = H_socket.Socket_Send, args = ( Socket_Client, Address, ClientLogFile ) )
	# 读取线程启动
	RecvThread.start()
	# 送信线程启动
	SendThread.start()

	# 等待线程结束
	# RecvThread.join()
	SendThread.join()

	# socket关闭
	print( "socket close!" )
	Socket_Client.close()

except KeyboardInterrupt :
	# ctrl + c
	print( 'you have Crtl+C, Now Quit!' )
	# socket关闭
	Socket_Client.close()
	# Log文件关闭
	ClientLogFile.close()

# except :
# 	# socket关闭
# 	Socket_Client.close()
# 	# Log文件关闭
# 	ClientLogFile.close()

raw_input()
